package ch.sv7.tool.dbmanager.db.fix;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import ch.sv7.tool.dbmanager.db.SchemaElement;
import ch.sv7.tool.dbmanager.db.dialect.Dialect;
import ch.sv7.tool.dbmanager.db.dialect.DialectException;
import ch.sv7.tool.dbmanager.db.difference.SchemaDifference;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public interface SchemaFix extends Comparable {
	
	// Drop priorities
	public static final int PRIORITY_FOREIGN_KEY_DROP = 1;
	public static final int PRIORITY_INDEX_DROP = 2;
	public static final int PRIORITY_UNIQUE_INDEX_DROP = 3;
	public static final int PRIORITY_PK_INDEX_DROP = 4;
	public static final int PRIORITY_CONSTRAINT_DROP = 5;
	public static final int PRIORITY_TABLE_DROP = 6;
	
	// Creation/Edit priorities
	public static final int PRIORITY_TABLE_CREATION = 7;
	
	public static final int PRIORITY_FOREIGN_KEY_RENAME = 8;
	public static final int PRIORITY_INDEX_RENAME = 9;
	public static final int PRIORITY_COLUMN_RENAME = 10;
	public static final int PRIORITY_COLUMN_CREATION = 11;
	public static final int PRIORITY_COLUMN_EXPLICIT_UPDATE = 12;
	public static final int PRIORITY_COLUMN_IMPLICIT_UPDATE = 13;
	public static final int PRIORITY_COLUMN_COPY = 14;
	
	public static final int PRIORITY_CONSTRAINT_CREATION = 15;
	public static final int PRIORITY_PK_INDEX_CREATION = 16;
	public static final int PRIORITY_UNIQUE_INDEX_CREATION = 17;
	public static final int PRIORITY_INDEX_CREATION = 18;
	public static final int PRIORITY_FOREIGN_KEY_CREATION = 19;

	// Actions
	public static final int ACTION_DROP = 0;
	public static final int ACTION_EDIT = 1;
	public static final int ACTION_CREATE = 2;
	public static final int ACTION_COPY_DATA = 3;
	
	public void setDialect(Dialect dialect);
	
	/**
	 * returns the action of the fix in order to be 
	 * able to remove incompatible fixes.
	 * Example: no need to edit a column if it will be recreated
	 * 
	 * @return an int representing the action 
	 */
	public int getAction();
	public SchemaElement getActionTarget();
	
	/**
	 * Sets the difference at the origin of this fix
	 * @param difference
	 */
	public void addFixedDifference(SchemaDifference difference);
	public void addFixedDifferences(List differences);
	public List getFixedDifferences();

	/**
	 * Returns the SQL to fix
	 * @return
	 * @throws DialectException
	 */
	public String getSQLFix() throws DialectException ;
	
	/**
	 * Executes the fix
	 * @param conn
	 * @throws DialectException
	 * @throws SQLException
	 */
	public void executeFixing(Connection conn) throws DialectException, SQLException;
	
	/**
	 * Returns the priority of the fix to be able to sort
	 * fixes properly. 
	 * @return
	 */
	public int getPriority();
	
	/**
	 * Returns a human-readable description of the fix
	 * @return
	 */
	public String getDescription();
}
